/*
 * Notifications
 *
 */

$(document).ready(function() {
    notifications.view_notification();
    //notifications.load_notification();
    notifications.showNoti();
    notifications.save_notifications();
    notifications.selectOne_noti();
    //user view
    var pathname = window.location.pathname;
    var user_view = $("#user_url_view").val();
    if (pathname== "/notifications" || pathname==user_view + "notifications") {
        user_notification.loadView();
    }
    
});

notifications = {
    view_notification: function() {

        $("body").on("click", ".show_notification", function() {
            $(".show_notification").popover({
                html: true,
                title: 'Notification',
                placement: 'bottom',
                //trigger: 'hov ',
                container: '',
                content: '<label style="color:#000;">Loading...</a>'
            }).popover('show');
            $.ajax({
                type: "POST",
                url: "/ajax/notification",
                dataType: 'json',
                data: {
                    action: "notificationPost"
                },
                cache: false,
                success: function(result) {
                    //console.log(result)
                    var ret = "";
                    var notify = "";
                    if (result != null) {
                        var noti = result[0].notification;
                        var noti_like = noti['like'];
                        var noti_post = noti['post'];
                        var noti_comment = noti['comment'];
                        var noti_tasks = noti['tasks'];
                        var noti_request = noti['request'];

                        var list = [];
                        var list2 = [];
                        var la = [];
                        // For post comment
                        if (noti_post != null) {
                            for (var a = 0; a < noti_post.length; a++) {
                                var n = noti_post[a];
                                $("body").data("notiData_" + n.notiID_encrypt, n);
                                ret += notiBody(n, result, "");
                                list[n.notiID] = notiBody(n, result);
                                //list2[n.notiID] = notiBody(n,result);
                                list2.push({"id": n.notiID, "value": notiBody(n, result, "")});
                                //list2.push({"a":notiBody(n,result)});
                            }
                        }

                        // For tasks
                        if (noti_tasks != null) {
                            for (var a = 0; a < noti_tasks.length; a++) {
                                var n = noti_tasks[a];
                                $("body").data("notiData_" + n.notiID_encrypt, n);
                                ret += notiBody(n, result, "");
                                list[n.notiID] = notiBody(n, result, "");
                                //list2[n.notiID] = notiBody(n,result);
                                list2.push({"id": n.notiID, "value": notiBody(n, result, "")});
                            }
                        }

                        // For Like
                        if (noti_like != null) {
                            for (var a = 0; a < noti_like.length; a++) {
                                var n = noti_like[a];
                                $("body").data("notiData_" + n.notiID_encrypt, n);
                                ret += notiBody(n, result, "");
                                list[n.notiID] = notiBody(n, result, "");
                                //list2[n.notiID] = notiBody(n,result);
                                list2.push({"id": n.notiID, "value": notiBody(n, result, "")});
                            }
                        }

                        // For request
                        if (noti_request != null) {
                            for (var a = 0; a < noti_request.length; a++) {
                                var n = noti_request[a];
                                $("body").data("notiData_" + n.notiID_encrypt, n);
                                ret += notiBody(n, result, "");
                                list[n.notiID] = notiBody(n, result, "");
                                //list2[n.notiID] = notiBody(n,result);
                                list2.push({"id": n.notiID, "value": notiBody(n, result, "")});
                            }
                        }



                        var gNoti = "";
                        //la.push(list2);


                        var l = list2.sort(function(a, b) {
                            return b.id - a.id;
                        });

                        //for (var i in list2) {
                        //    console.log(i)
                        //}
                        for (var a = 0; a < l.length; a++) {
                            //console.log(l[a]['id'])
                            gNoti += l[a]['value'];
                        }
                        notify = getNoty(gNoti);

                    } else {
                        var notify = "<lable style='color:#000'>No notification</label>";
                    }

                    $(".show_notification").data('popover').options.content = notify;
                    $(".show_notification").popover('show');
                    $("label.timeago").timeago();  // Time To go
                    $(".notification-container").perfectScrollbar();

                }
            });




        });
        $("body").on("mouseleave", ".popover", function() {
            $(".show_notification").popover('hide');
        });
    },
    load_notification: function() {
        $.ajax({
            type: "POST",
            url: "/ajax/notification",
            dataType: 'json',
            data: {
                action: "notificationPost"
            },
            cache: false,
            success: function(result) {
                $("body").data("user_notification", result);
            }
        });
    },
    showNoti: function() {
        $("body").on("click", ".showNoti", function() {
            var encType = $(this).attr("data-encrypt");
            var data = $("body").data("notiData_" + encType);
            //console.log(data)
            //console.log(data.postID_encrypt)
            //console.log(data.PostedBy)
            //console.log(data.notiID_encrypt)
            //
            $.ajax({
                type: "POST",
                url: "/ajax/notification",
                data: {
                    action: "updateNoti",
                    notiID: data.notiID
                },
                success: function(e) {
                    //console.log(data)
                    var pathname = window.location.pathname;
                    var split_pathname = pathname.split("/");
                    var user_view = $("#user_url_view").val();
                    if (split_pathname[2]=="notifications") {
                        var url_user = user_view;
                    }else{
                        var url_user = "/";
                    }
                    if (data.type == 1) {
                        window.location.replace(url_user + 'post?view_type=view_post&postID=' + data.notiID_encrypt);
                    } else if (data.type == 2) {
                        window.location.replace(url_user + 'post?view_type=view_post&postID=' + data.postID_encrypt);
                    } else if (data.type == 6) {
                        window.location.replace(url_user + 'post?view_type=view_post&postID=' + data.postID_encrypt);
                    } else if (data.type == 1 || data.type == 2) {
                        window.location.replace(url_user + 'post?view_type=view_post&postID=' + data.postID_encrypt);
                    } else if (data.type == 3 || data.type == 4) {
                        window.location.replace(url_user + 'post?view_type=view_post&postID=' + data.postID_encrypt);
                    } else if (data.type == 5) {
                        window.location.replace(url_user + 'workspace?view_type=request&formID=' + data.formID + '&requestID=' + data.requestID);
                    } else if (data.type == 10 || data.type == 11 || data.type == 12 || data.type == 13) {
                        window.location.replace(url_user + 'task?view_type=view_task&taskID=' + data.infoID);
                    }
                }
            });
        });
    },
    save_notifications: function() {
        $('.save_notifications').click(function() {

            var obj = [];
            obj.push(GetDetails());
            var json_details = JSON.stringify(obj)
            console.log(json_details)

            con = "Are you sure do you want to save this?";
         /*  var newConfirm = new */ jConfirm(con, 'Confirmation Dialog', '', '', '', function(r) {
                if (r == true) {
                    $.post("/ajax/settings", {action: "saveNotifications", json_details: json_details}, function(data) {
                        if (data == "Done.") {
                      /* var newConfirm = new*/ jConfirm("Setting was successfully saved.", "", "", "", "", "");
                        //newConfirm.themeConfirm("confirm2",{'icon':'<i class="fa fa-exclamation-triangle fl-margin-right" style="color:#f00; font-size:15px;"></i>'});
                        } else {
                         /*var newConfirm = new*/ jConfirm("Setting was successfully updated.", "", "", "", "", "");
                        //newConfirm.themeConfirm("confirm2",{'icon':'<i class="fa fa-exclamation-triangle fl-margin-right" style="color:#f00; font-size:15px;"></i>'});   
                        }
                        if ($("input[class='selectCheckBox_noti']:checked").length == "9") {
                            $(".selectAll_noti").attr("checked", true);
                        } else {
                            $(".selectAll_noti").attr("checked", false);
                        }
                    });
                }
            });
           //newConfirm.themeConfirm("confirm2",{'icon':'<i class="fa fa-exclamation-triangle fl-margin-right" style="color:#f00; font-size:15px;"></i>'});

        });
    },
    selectOne_noti: function() {
        // Delete now the message
        $(".selectCheckBox_noti").on("click", function() {
            if (this.checked) {
                $(this).attr('data-value', "1");
            } else {
                $(this).attr('data-value', "0");
            }
        });

        $(".selectAll_noti").on("click", function() {
            if (this.checked) {
                $(".selectCheckBox_noti").each(function() {
                    $(this).prop("checked", true)
                    $(this).attr('data-value', "1");
                });
            } else {
                $(".selectCheckBox_noti").each(function() {
                    $(this).prop("checked", false)
                    $(this).attr('data-value', "0");
                });
            }
        });
    }
    // Allow Notifications Checkbox
}
function GetDetails() {
    var arr = [];
    var arr_1 = [];
    var arr_2 = [];
    var arr_3 = [];
    var arr_4 = [];
    var arr_5 = [];
    var arr_6 = [];
    var arr_json = {};
    var noti_json = {};
    var arr_count = [];
    var count_checked = $('.selectCheckBox_noti:checked').length;

    $('.selectCheckBox_noti').each(function() {
        var v_value = $(this).attr('data-value');
        var c_name = $(this).attr('data-name');
        var noti_name = $(this).attr("data-noti-name");

        if (noti_name == "Comment") {
            arr.push(v_value);
            noti_json[noti_name] = arr;
        } else if (noti_name == "Like") {
            arr_1.push(v_value);
            noti_json[noti_name] = arr_1;
        } else if (noti_name == "Tagged") {
            arr_2.push(v_value);
            noti_json[noti_name] = arr_2;
        } else if (noti_name == "Message") {
            arr_3.push(v_value);
            noti_json[noti_name] = arr_3;
        } else if (noti_name == "Request") {
            arr_4.push(v_value);
            noti_json[noti_name] = arr_4;
        }
    });
    if ($('.select_summar_noti').is(":checked") == true) {
        $summary_noti = "1";
    } else {
        $summary_noti = "0";
    }
    //$summary_notification = $('.selectAll_snoti:checked').map(function(){return}).
    arr_6.push($summary_noti);
    noti_json['summary_notification'] = arr_6;
    arr_5.push(count_checked);
    noti_json['count_checked'] = arr_5;
    arr_json['notifications'] = noti_json;
    return arr_json;
}
function sortmyway(data_A, data_B)
{
    return (data_A - data_B);
}
//
function notiBody(noti, result, nType) {
    var ret = "";

    var countLength = result.length;

    if (noti.type == 1) {
        var msg = '<b>' + noti.name + '</b> &nbsp; mentioned you in a post.';

        var icon_noti = '<i class="icon-file"></i>';

        // Comment on post
    } else if (noti.type == 2) {

        if (noti.countCommentPost == 1 && noti.postedIDBy != noti.authID && noti.notiUserID != noti.postedIDBy) {
            var msg = '<b>' + noti.name + '</b> &nbsp; commented on ' + noti.PostedBy + '`s post.';
        } else if (noti.countCommentPost > 1 && noti.postedIDBy != noti.authID && noti.notiUserID != noti.postedIDBy) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also commented on ' + noti.PostedBy + '`s post.';
        } else if (noti.countCommentPost == 1 && noti.postedIDBy == noti.authID && noti.notiUserID != noti.postedIDBy) {
            var msg = '<b>' + noti.name + '</b> &nbsp; commented on your post.';
        } else if (noti.countCommentPost > 1 && noti.postedIDBy == noti.authID && noti.notiUserID != noti.postedIDBy) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also commented on your post.';
        } else if (noti.countCommentPost > 1 && noti.name == noti.PostedBy && noti.notiUserID != noti.postedIDBy) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also commented on your post.';
        } else if (noti.countCommentPost > 1 && noti.name == noti.PostedBy && noti.notiUserID == noti.postedIDBy
                || noti.countCommentPost == 1 && noti.name == noti.PostedBy && noti.notiUserID == noti.postedIDBy) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also commented its post.';
        } else {
            var msg = '<b>' + noti.name + '</b> &nbsp; commented on post.';
        }

        var icon_noti = '<i class="icon-comment"></i>';

    } else if (noti.type == 3) {
        var lType = "post";
        var like = noti.countLike;


        if (like == 1 && noti.postedIDBy != noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; like on ' + noti.PostedBy + '`s ' + lType + '.';
        } else if (like > 1 && noti.postedIDBy != noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also like on ' + noti.PostedBy + '`s ' + lType + '.';
        } else if (like == 1 && noti.postedIDBy == noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; like on your ' + lType + '.';
        } else if (like > 1 && noti.postedIDBy == noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also like on your ' + lType + '.';
        } else {
            var msg = '<b>' + noti.name + '</b> &nbsp; like on your ' + lType + '.';
        }

        var icon_noti = '<i class="icon-thumbs-up"></i>';

    } else if (noti.type == 4) {

        var lType = "comment";
        var like = noti.countLikeReply;


        if (like == 1 && noti.postedIDBy != noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; like on ' + noti.PostedBy + '`s ' + lType + '.';
        } else if (like > 1 && noti.postedIDBy != noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also like on ' + noti.PostedBy + '`s ' + lType + '.';
        } else if (like == 1 && noti.postedIDBy == noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; like on your ' + lType + '.';
        } else if (like > 1 && noti.postedIDBy == noti.authID) {
            var msg = '<b>' + noti.name + '</b> &nbsp; also like on your ' + lType + '.';
        } else {
            var msg = '<b>' + noti.name + '</b> &nbsp; like on your ' + lType + '.';
        }

        var icon_noti = '<i class="icon-thumbs-up"></i>';

        // Request    
    } else if (noti.type == 5) {
        var msg = '<b>' + noti.name + '</b> &nbsp; wants your approval';

        var icon_noti = '<i class="icon-reorder"></i>';
    } else if (noti.type == 6) {
        var msg = '<b>' + noti.comment + '</b> &nbsp; mentioned you in a comment.';

        var icon_noti = '<i class="icon-comment"></i>';

    }

    // For Task
    else if (noti.type == 10) {
        var msg = '<b>' + noti.name + '</b> &nbsp; added as a team in task.';

        var icon_noti = '<i class="icon-reorder"></i>';
    } else if (noti.type == 11) {
        var msg = '<b>' + noti.name + '</b> &nbsp; set you as an editor in task.';
        var icon_noti = '<i class="icon-reorder"></i>';

    } else if (noti.type == 12) {
        var msg = '<b>' + noti.name + '</b> &nbsp; set you as a viewer in task.';

        var icon_noti = '<i class="icon-reorder"></i>';

    } else if (noti.type == 13) {
        var msg = '<b>' + noti.name + '</b> &nbsp; A task was not able to complete on its due date.';

        var icon_noti = '<i class="icon-reorder"></i>';

    }

    //console.log(noti.user_view);
    //ret += '<a href="/post?view_type=view_post&postID=' + noti.postID_encrype + '" target="_blank">';
    ret += '<div style="float:left;width:100%;border-bottom: 1px solid #ddd; ' + noti.bg + '" class="showNoti" data-encrypt="' + noti.notiID_encrypt + '">';
    ret += '<div  class="padding_5">';
    ret += '<div class="pull-left" style="margin-top:5px;width: 22%;">';
    ret += '<div class="pull-left " style="width: 44px;height: 44px;">';
    ret += noti.image;
    ret += '</div>';
    ret += '</div>';
    ret += '<div class="pull-left" style="width: 70%;margin-top:5px;margin-left: 10px;color:#000;">';
    ret += msg;
    ret += '<br>';
    ret += icon_noti + ' <label class="timeago" title="' + noti.notiDate + '" style="font-size:10px;color:#ddd;"></label>';
    ret += '</div>';
    ret += '</div>';
    ret += '</div>';
    //ret += '</a>';

    if (nType!="") {
        return msg;
    }else{
        return ret;
    }
    
}

function getNoty(noti) {

    var ret = "";

    ret += '<div class="notification-container" style="position: relative; margin: 0px auto; padding: 0px; width: 100%; height: 200px;overflow: hidden;">';
    ret += noti;
    ret += '</div>';
    return ret;
}


var user_notification = {
    loadView: function() {
        var self = this;
        var pathname = window.location.pathname;
        var user_view = $("#user_url_view").val();
        var split_pathname = pathname.split("/");
        if (split_pathname[2]!="notifications") {
            ui.block();
        }
        
        $.ajax({
            type: "POST",
            url: "/ajax/notification",
            dataType: 'json',
            data: {
                action: "notificationPost"
            },
            cache: false,
            success: function(result) {

                console.log(result);
                var comments = result[0].notification.comment;
                var likes = result[0].notification.like;
                var posts = result[0].notification.post;
                var tasks = result[0].notification.tasks;
                var requests = result[0].notification.request;
                var html = '';
                var today_data = [];
                var yesterday_data = [];
                var past_data = [];
                var today = new Date();
                var yesterday = new Date();
                var yesterday = yesterday.adjustDay(-1);
                var notifDate;
                var path_user_view = $("#user_url_view").val();
                
                
                //Comment
                for (var index in comments) {
                    $("body").data("notiData_" + comments[index].notiID_encrypt, comments[index]);
                    var msg = notiBody(comments[index], result, "userNoti");
                    notifDate = new Date(comments[index].date);
                    // view or not view
                    if (comments[index].bg=="") {
                        var exPoint = "";
                    }else{
                        var exPoint = '<i class="icon-exclamation-sign fa fa-exclamation-circle fl-notification-color"></i>';
                    }
                    
                    // View
                    if (comments[index].auth_level_id == "2") {
                        var path = 'post?view_type=view_post&postID=' + comments[index].notiID_encrypt;
                    }else{
                        var path = path_user_view + 'post?view_type=view_post&postID=' + comments[index].notiID_encrypt;
                    }
                    
                    html = '<li class="showNoti" data-encrypt="' + comments[index].notiID_encrypt + '"><div class="fl-status-and-type">' + exPoint + '<span class="fl-from">Comment</span></div><div class="fl-notification-content"><a href="' + path + '">' +
                            msg + '</div><div class="fl-notification-del-time"><span class="fl-delete"></span> <label class="time timeago" title="' + comments[index].date + '"></label></div><div class="clearfix"></div></li>';


                    if (today.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        today_data.push({
                            date: comments[index].date,
                            html: html
                        });
                    } else if (yesterday.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        yesterday_data.push({
                            date: comments[index].date,
                            html: html
                        });
                    } else {
                        past_data.push({
                            date: comments[index].date,
                            html: html
                        });
                    }
                }
                
                
                
                
                //requests
                
                for (var index in requests) {
                    $("body").data("notiData_" + requests[index].notiID_encrypt, requests[index]);
                    var msg = notiBody(requests[index], result, "userNoti");
                    notifDate = new Date(requests[index].date.replace(/\-/g,"/"));
                    // console.log(notifDate)
                    // var d = requests[index].date;
                    // notifDate = new Date(d.substr(0, 4), d.substr(5, 2) - 1, d.substr(8, 2), d.substr(11, 2), d.substr(14, 2), d.substr(17, 2));
                    // view or not view
                    if (requests[index].bg=="") {
                        var exPoint = "";
                    }else{
                        var exPoint = '<i class="icon-exclamation-sign fa fa-exclamation-circle fl-notification-color"></i>';
                    }
                    
                    // View
                    if (requests[index].auth_level_id == "2") {
                        var path ="workspace?view_type=request&formID=" + requests[index].formID + '&requestID=' + requests[index].requestID; 
                    }else{
                        var path = path_user_view + "workspace?view_type=request&formID=" + requests[index].formID + '&requestID=' + requests[index].requestID; 
                    }
                    
                    
                    html = '<li class="showNoti" data-encrypt="' + requests[index].notiID_encrypt + '"><div class="fl-status-and-type">' + exPoint + '<span class="fl-from">Request</span></div><div class="fl-notification-content"><a>' + requests[index].Requestor_name +
                            '</a> submitted a request with Tracking Number <b>"' +
                            requests[index].TrackNo + '"</b> for your action.</div><div class="fl-notification-del-time"><span class="fl-delete"></span> <label class="time timeago" title="' + requests[index].date + '"></label></div><div class="clearfix"></div></li>';


                    if (today.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        today_data.push({
                            date: requests[index].date,
                            html: html
                        });
                    } else if (yesterday.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        yesterday_data.push({
                            date: requests[index].date,
                            html: html
                        });
                    } else {
                        past_data.push({
                            date: requests[index].date,
                            html: html
                        });
                    }

                }
                //posts
                for (var index in posts) {
                    $("body").data("notiData_" + posts[index].notiID_encrypt, posts[index]);
                    var msg = notiBody(posts[index], result, "userNoti");
                    notifDate = new Date(posts[index].date.replace(/\-/g,"/"));
                    // view or not view
                    if (posts[index].bg=="") {
                        var exPoint = "";
                    }else{
                        var exPoint = '<i class="icon-exclamation-sign fa fa-exclamation-circle fl-notification-color"></i>';
                    }
                    // View
                    if (posts[index].auth_level_id == "2") {
                        var path = 'post?view_type=view_post&postID=' + posts[index].postID_encrypt;
                    }else{
                        var path = path_user_view + 'post?view_type=view_post&postID=' + posts[index].postID_encrypt;
                    }
                    
                    html = '<li class="showNoti" data-encrypt="' + posts[index].notiID_encrypt + '"><div class="fl-status-and-type">' + exPoint + '<span class="fl-from">Announcement</span></div><div class="fl-notification-content"><a href="' + path + '">' +
                            msg + '</div><div class="fl-notification-del-time"><span class="fl-delete"></span> <label class="time timeago" title="' + posts[index].date + '"></label></div><div class="clearfix"></div></li>';


                    if (today.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        today_data.push({
                            date: posts[index].date,
                            html: html
                        });
                    } else if (yesterday.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        yesterday_data.push({
                            date: posts[index].date,
                            html: html
                        });
                    } else {
                        past_data.push({
                            date: posts[index].date,
                            html: html
                        });
                    }
                }
                //likes
                for (var index in likes) {
                    $("body").data("notiData_" + likes[index].notiID_encrypt, likes[index]);
                    var msg = notiBody(likes[index], result, "userNoti");
                    notifDate = new Date(likes[index].date.replace(/\-/g,"/"));
                    // view or not view
                    if (likes[index].bg=="") {
                        var exPoint = "";
                    }else{
                        var exPoint = '<i class="icon-exclamation-sign fa fa-exclamation-circle fl-notification-color"></i>';
                    }
                    // View
                    if (likes[index].auth_level_id == "2") {
                        var path = 'post?view_type=view_post&postID=' + likes[index].postID_encrypt;
                    }else{
                        var path = path_user_view + 'post?view_type=view_post&postID=' + likes[index].postID_encrypt;
                    }
                    html = '<li class="showNoti" data-encrypt="' + likes[index].notiID_encrypt + '"><div class="fl-status-and-type">' + exPoint + '<span class="fl-from">Announcement</span></div><div class="fl-notification-content"><a href="' + path + '">' +
                            msg + '</div><div class="fl-notification-del-time"><span class="fl-delete"></span> <label class="time timeago" title="' + likes[index].date + '"></label></div><div class="clearfix"></div></li>';


                    if (today.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        today_data.push({
                            date: likes[index].date,
                            html: html
                        });
                    } else if (yesterday.setHours(0, 0, 0, 0) == notifDate.setHours(0, 0, 0, 0)) {
                        yesterday_data.push({
                            date: likes[index].date,
                            html: html
                        });
                    } else {
                        past_data.push({
                            date: likes[index].date,
                            html: html
                        });
                    }
                }
                console.log(today_data)
                today_data = today_data.sort(self.sortByDate('date')); //sort 
                yesterday_data = yesterday_data.sort(self.sortByDate('date'));//sort
                past_data = past_data.sort(self.sortByDate('date'));//sort

                $('.notification_today').html('');
                for (var index in today_data) {
                    $('.notification_today').append(today_data[index].html);
                    $("label.timeago").timeago();  // Time To go
                }

                if (today_data.length == 0) {
                    $('.notification_today').html('<div class="fl-no-record-found"><h1><i class="fa fa-files-o"></i> No Record Found</h1></div>');
                }

                $('.notification_yesterday').html('');
                for (var index in yesterday_data) {
                    $('.notification_yesterday').append(yesterday_data[index].html);
                    $("label.timeago").timeago();  // Time To go
                }

                if (yesterday_data.length == 0) {
                    $('.notification_yesterday').html('<div class="fl-no-record-found"><h1><i class="fa fa-files-o"></i> No Record Found</h1></div>');
                }

                $('.notification_pastdays').html('');
                for (var index in past_data) {
                    $('.notification_pastdays').append(past_data[index].html);
                    $("label.timeago").timeago();  // Time To go
                }

                if (past_data.length == 0) {
                    $('.notification_pastdays').html('<div class="fl-no-record-found"><h1><i class="fa fa-files-o"></i> No Record Found</h1></div>');
                }

                $('.fl-notification-wrapper-scroll').perfectScrollbar({
                    wheelSpeed: 20,
                    wheelPropagation: false,
                    suppressScrollX: true
                });
                ui.unblock();
            }
        });
        
    },
    sortByDate: function(prop) {
        return function(a, b) {
            var aDate = new Date(a[prop].replace(/\-/g,"/"));
            var bDate = new Date(b[prop].replace(/\-/g,"/"));
            if (aDate > bDate) {
                return -1;
            } else if (aDate < bDate) {
                return 1;
            }
            return 0;
        }
    }
};